﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.03.16
 * 描 述：window窗口皮肤	
 */
var bootstrap = function ($, learun) {
    "use strict";

    // 菜单操作
    var meuns = {
        init: function () {
            var $menuwarp = $('.lr-frame-menu');
            $menuwarp.append('<div class="lr-second-menu-wrap" id="lr_second_menu_wrap"></div><div class="lr-frame-menu-overlay" id="lr_frame_menu_overlay"></div>');
            this.load();
            this.bind();
        },
        load: function () {
            var modulesTree = learun.clientdata.get(['modules']);
            // 第一级菜单
            var modules = modulesTree || [];
            var $firstmenus = $('<ul class="lr-first-menu-list"></ul>');
            for (var i = 0, l = modules.length; i < l; i++) {
                var item = modules[i];
                if (item.isMenu == 1) {
                    var $applistul = $('<ul></ul>');// 应用列表
                    if (item.id != 'expand') {
                        $applistul.append(meuns.getAppItem(item));
                    }
                    var $firstMenuItem = $('<li></li>');
                    if (!!item.name) {
                        $firstMenuItem.attr('title', item.name);
                    }
                    var menuItemHtml = '<a id="' + item.id + '" href="javascript:void(0);" class="lr-menu-item">';
                    menuItemHtml += '<i class="' + item.css + ' lr-menu-item-icon"></i>';
                    menuItemHtml += '<span class="lr-menu-item-text">' + item.name + '</span>';
                    menuItemHtml += '<span class="lr-menu-item-arrow"></span></a>';
                    $firstMenuItem.append(menuItemHtml);
                    // 第二级菜单
                    var secondModules = item.subMenus || [];
                    var $secondMenus = $('<ul class="lr-second-menu-list"></ul>');
                    var secondMenuHad = false;
                    for (var j = 0, sl = secondModules.length ; j < sl; j++) {
                        var secondItem = secondModules[j];
                        if (secondItem.isMenu == 1) {

                            if (secondItem.target != 'expand') {
                                $applistul.append(meuns.getAppItem(secondItem));
                            }

                            secondMenuHad = true;
                            var $secondMenuItem = $('<li></li>');
                            if (!!secondItem.name) {
                                $secondMenuItem.attr('title', secondItem.name);
                            }
                            var secondItemHtml = '<a id="' + secondItem.id + '" href="javascript:void(0);" class="lr-menu-item" >';
                            secondItemHtml += '<i class="' + secondItem.css + ' lr-menu-item-icon"></i>';
                            secondItemHtml += '<span class="lr-menu-item-text">' + secondItem.name + '</span>';
                            secondItemHtml += '</a>';

                            $secondMenuItem.append(secondItemHtml);
                            // 第三级菜单
                            var threeModules = secondItem.subMenus || [];
                            var $threeMenus = $('<ul class="lr-three-menu-list"></ul>');
                            var threeMenuHad = false;
                            for (var m = 0, tl = threeModules.length ; m < tl; m++) {
                                var threeItem = threeModules[m];
                                if (threeItem.isMenu == 1) {
                                    if (threeItem.target != 'expand') {
                                        $applistul.append(meuns.getAppItem(threeItem));
                                    }

                                    threeMenuHad = true;
                                    var $threeMenuItem = $('<li></li>');
                                    $threeMenuItem.attr('title', threeItem.name);
                                    var threeItemHtml = '<a id="' + threeItem.id + '" href="javascript:void(0);" class="lr-menu-item" >';
                                    threeItemHtml += '<i class="' + threeItem.css + ' lr-menu-item-icon"></i>';
                                    threeItemHtml += '<span class="lr-menu-item-text">' + threeItem.name + '</span>';
                                    threeItemHtml += '</a>';
                                    $threeMenuItem.append(threeItemHtml);
                                    $threeMenus.append($threeMenuItem);
                                }
                            }
                            if (threeMenuHad) {
                                $secondMenuItem.addClass('lr-meun-had');
                                $secondMenuItem.find('a').append('<span class="lr-menu-item-arrow"><i class="fa fa-angle-left"></i></span>');
                                $secondMenuItem.append($threeMenus);
                            }
                            $secondMenus.append($secondMenuItem);
                        }
                    }
                    if (secondMenuHad) {
                        $secondMenus.attr('data-value', item.id);
                        $('#lr_second_menu_wrap').append($secondMenus);
                    }
                    $firstmenus.append($firstMenuItem);
                    if ($applistul.find('li').length > 0) {
                        $("#lr_applist_slidebox").append($applistul);
                        $(".lr-applist-slidebox-slider-content").append('<li><i class="fa fa-circle"></i></li>');
                    }

                }
            }
            $('#lr_frame_menu').html($firstmenus);

            // 语言包翻译
            $('.lr-menu-item-text').each(function () {
                var $this = $(this);
                var text = $this.text();
                learun.language.get(text, function (_text) {
                    $this.text(_text);
                    $this.parent().parent().attr('title', _text);
                });
            });
            $('.icon-text').each(function () {
                var $this = $(this);
                var text = $this.text() || '';
                learun.language.get(text.replace(/^\s+|\s+$/g, ""), function (_text) {
                    $this.text(_text);
                });
            });
        },
        bind: function () {
            $("#lr_frame_menu").lrscroll();
            $("#lr_second_menu_wrap").lrscroll();
            $('#lr_windows_start').on('click', meuns.startMenuClick);
            $('#lr_frame_menu_overlay').on('click', meuns.startMenuClick);
            // 添加点击事件
            $('#lr_frame_menu a').on('click', function () {
                var $obj = $(this);
                var id = $obj.attr('id');
                var _module = learun.clientdata.get(['modulesMap', id]);
                switch (_module.target) {
                    case 'iframe':// 窗口
                        meuns.startMenuClick();
                        setTimeout(function () {
                            if (learun.validator.isNotNull(_module.path).code) {
                                learun.frameTab.open(_module);
                            }
                            else {

                            }
                        }, 250);
                        break;
                    case 'expand':
                        var $li = $obj.parent();
                        if (!$li.hasClass('active')) {
                            $('#lr_frame_menu li.active').removeClass('active');
                            $li.addClass('active');
                            $('#lr_second_menu_wrap .lr-second-menu-list').hide();
                            $('#lr_second_menu_wrap .lr-second-menu-list[data-value="' + _module.id + '"]').show();
                        }
                        break;
                }
            });

            $('#lr_second_menu_wrap a').on('click', function () {
                var $obj = $(this);
                var id = $obj.attr('id');
                var _module = learun.clientdata.get(['modulesMap', id]);
                switch (_module.target) {
                    case 'iframe':// 窗口
                        meuns.startMenuClick();
                        setTimeout(function () {
                            if (learun.validator.isNotNull(_module.path).code) {
                                learun.frameTab.open(_module);
                            }
                            else {

                            }
                        }, 250);
                        break;
                    case 'expand':
                        var $ul = $obj.next();
                        if ($ul.is(':visible')) {
                            $ul.slideUp(500, function () {
                                $obj.removeClass('open');
                            });
                        }
                        else {
                            $ul.parents('.lr-second-menu-list').find('.lr-three-menu-list').slideUp(300, function () {
                                $(this).prev().removeClass('open');
                            });
                            $ul.slideDown(300, function () {
                                $obj.addClass('open');
                            });
                        }
                        break;
                }
            });

            $('.lr-first-menu-list>li').eq(0).find('a').trigger('click');


            $(".lr-applist-slidebox-slider-content li").click(function () {
                var $this = $(this);
                if (!$this.hasClass("active")) {
                    var $oldli = $(".lr-applist-slidebox-slider-content li.active");
                    $oldli.removeClass("active");
                    $this.addClass("active");

                    var oldindex = $oldli.index();
                    var index = $(this).index();

                    $("#lr_applist_slidebox ul").eq(oldindex).hide();
                    $("#lr_applist_slidebox ul").eq(index).fadeIn("slow");
                }
            });

            $('#lr_applist_btn').on('click', function () {
                meuns.openApplist();
            });

            $('#lr_applist_slidebox .appItem').on('click', function () {
                var $obj = $(this);
                var id = $obj.attr('data-id');
                var _module = learun.clientdata.get(['modulesMap', id]);
                if (learun.validator.isNotNull(_module.path).code) {
                    learun.frameTab.open(_module);
                }
            });
            $('#lr_applist_content').hide();
            $(".lr-applist-slidebox-slider-content li").eq(0).trigger('click');
           
            //learun.frameTab.leaveFocus();

          
        },
        startMenuClick: function () {
            var $lr_frame_menu = $('.lr-frame-menu');
            if ($lr_frame_menu.is(':visible')) {
                $lr_frame_menu.slideUp(300);
            }
            else {
                $lr_frame_menu.show();
            }
        },
        getAppItem: function (item) {
            var colorindex = Math.round(Math.random() * 9 + 1);
            var _html = '';
            _html += '<li class="appItem" data-id="' + item.id + '" href="' + item.path + '">';
            _html += '<div class="icon" >';
            _html += '<i class="fa ' + item.F_Icon + '"></i>';
            _html += '</div>';
            _html += '<div class="icon-text">';
            _html +=item.F_FullName;
            _html += '</div>';
            _html += '</li>';
            return _html;
        },
        closeApplist: function () {
            var appBtn = $('#lr_applist_btn');
            if (!appBtn.hasClass('off')) {
                $('#lr_applist_btn').addClass('off');
                $('#lr_applist_content').hide();
            }
        },
        openApplist: function () {
            var appBtn = $('#lr_applist_btn');
            if (appBtn.hasClass('off')) {
                learun.frameTab.leaveFocus();
                $('#lr_applist_btn').removeClass('off');
                $('#lr_applist_content').show();
            }
        }
    };

    learun.frameTab.opencallback = function () {
        meuns.closeApplist();
    };
    learun.frameTab.closecallback = function () {
        if (learun.frameTab.iframeId == '') {
            meuns.openApplist();
        }
    };

    meuns.init();

    var companyMap = {};
    var departmentMap = {};
    var userMap = {};
    
    var imUserId = '';

    var getHeadImg = function (user) {
        var url = "";
        switch (user.img) {
            case '0':
                url += '/assets/plugins/images/head/on-girl.jpg';
                break;
            case '1':
                url += '/assets/plugins/images/head/on-boy.jpg';
                break;
            default:
                url += config.base_server() + 'users/' + user.id + '/getHeadImg?access_token=' + config.getToken().access_token;
                break;
        }
        return url;
    };
    // 发送聊天信息
    var sendMsg = function (msg, time) {
        var loginInfo = learun.clientdata.get(['userinfo']);
        learun.clientdata.getAsync('user', {
            key: loginInfo.user.id,
            callback: function (data, op) {
                data.id = op.key;
                var _html = '\
                <div class="me im-time">'+ (time || '') + '</div>\
                <div class="im-me">\
                    <div class="headimg"><img src="'+ getHeadImg(data) + '"></div>\
                    <div class="arrow"></div>\
                    <span class="content">'+ msg + '</span>\
                </div>';

                $('.lr-im-msgcontent .lr-scroll-box').append(_html);
                $('.lr-im-msgcontent').lrscrollSet('moveBottom');
            }
        });
    };
    // 接收聊天消息
    var revMsg = function (userId, msg, time) {
        learun.clientdata.getAsync('user', {
            key: userId,
            callback: function (data, op) {
                data.id = op.key;
                var _html = '\
                <div class="im-time">'+ (time || '') + '</div>\
                <div class="im-other">\
                    <div class="headimg"><img src="'+ getHeadImg(data) + '"></div>\
                    <div class="arrow"></div>\
                    <span class="content">'+ msg + '</span>\
                </div>';


                $('.lr-im-msgcontent .lr-scroll-box').append(_html);
                $('.lr-im-msgcontent').lrscrollSet('moveBottom');
            }
        });
    };

    var getTime = function (time) {
        var d = new Date();
        var c = d.DateDiff('d', time);
        if (c <= 1) {
            return learun.formatDate(time, 'hh:mm:ss');
        }
        else {
            return learun.formatDate(time, 'yyyy/MM/dd');
        }
    }
};